package es.goldbittle.android;

import es.goldbittle.android.R.id;
import quinielaGold.beans.DatosGenerales;
import quinielaGold.beans.Persona;
import quinielaGold.beans.Quiniela;
import quinielaGold.beans.Usuario;
import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
//import android.widget.NumberPicker;
import android.widget.ArrayAdapter;
import android.widget.ListView;
//import android.widget.NumberPicker;
import android.widget.AdapterView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;

public class QGSeleccionarTemporadaJornada extends Activity {
    /** Called when the activity is first created. */
	private QuiniGoldApp appQG;
	private int miOrigen;
	int[] lTemporadas;
	String[] datosTemporada = null;
	int[] lJornadas;
	String[] datosJornada  = null;
	String[] datosJornadaAux  = null;
	private DatosGenerales datosGen;	
	private int[] colors = new int[] { 0xFF282828, 0xFF505050 };
	
	ArrayAdapter<String> adaptadorJornada = null;
	Spinner cmbJornada = null;
	Spinner cmbTemporada =null;
	
	private void cargaSpinnerJornada(int pos){

		try
		{
			cmbJornada = (Spinner)findViewById(R.id.CmbJornada);
		
		int cont = 0;
        if (appQG.getJornadas()!=null)
        {
        	lJornadas = appQG.getJornadas();
        	for (int i = 0; i < lJornadas.length; i++)
        		if (lJornadas[i] == lTemporadas[pos])
        			cont++;
        	
        	datosJornada = new String[cont];
        	datosJornadaAux = new String[cont];
        	
        	cont = 0;
        	for (int i = 0; i < lJornadas.length; i++){
        		if (lJornadas[i] == lTemporadas[pos]){
        			datosJornadaAux[cont]= Integer.toString(lJornadas[i+1]);
        			cont++;
        		}
        		
        		i++;
        	}
        }
        else
        {
        	datosJornadaAux = new String[]{"1","2","3","4","5", "6", "7", "8", "9", "10", "11","12","13","14","15", "16", "17", "18", "19", "20", "21","22","23","24","25", "26", "27", "28", "29", "30", "31","32","33","34","35", "36", "37", "38", "39","40" };
        }
		
        if (datosGen.getTemporada() == lTemporadas[pos]){
	        for (int i = 0; i < datosJornadaAux.length; i++)
	        	datosJornada[datosJornadaAux.length - i -1] = datosJornadaAux[i];
        }
        else
        	datosJornada = datosJornadaAux;
        	
        
        
		//adaptadorJornada = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, datosJornada);
        adaptadorJornada = new ArrayAdapter<String>(this, R.layout.textview_spinner, datosJornada);
		cmbJornada.setAdapter(adaptadorJornada);    	   
		cmbJornada.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    		public void onItemSelected(AdapterView<?> parent, android.view.View v, int position, long id) {
    			appQG.setJornadaSeleccionada(Integer.parseInt(datosJornada[position]));
    		}
    		public void onNothingSelected(AdapterView<?> parent) {
    			appQG.setJornadaSeleccionada(0);
    		}
    		});   
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		
        
	}
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
		try{
        super.onCreate(savedInstanceState);
        //Elimina la barra del titulo superior
        requestWindowFeature(Window.FEATURE_NO_TITLE); 
        setContentView(R.layout.seleccionartemjor);

       	cmbTemporada = (Spinner)findViewById(R.id.CmbTemporada);
       	cmbJornada = (Spinner)findViewById(R.id.CmbJornada);
        

        appQG = ((QuiniGoldApp)getApplicationContext());
        datosGen = appQG.getDatosGenerales();

        if (datosGen == null)
        {
        	datosGen = appQG.obtenerDatosGenerales();
        }
        
        miOrigen = appQG.getOrigen();
        
//       if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
        
        if (appQG.getTemporadas() == null)
        {
        	lTemporadas = appQG.obtenerTemporadas();
        	appQG.setTemporadas(lTemporadas);
        }
        
        
        if (appQG.getTemporadas() !=null)
        {
        	lTemporadas = appQG.getTemporadas();
            if (appQG.getJornadas() == null)
            {
            	lJornadas = appQG.obtenerJornadas();
            	appQG.setJornadas(lJornadas);
            }
            int cont = 0;
            if (appQG.getJornadas()!=null)
            {
            	lJornadas=appQG.getJornadas();
            	for (int i = 0; i < lJornadas.length; i++)
            		if (lJornadas[i] == lTemporadas[0])
            			cont++;
            	
            	datosJornada = new String[cont];
            	
            	cont = 0;
            	for (int i = 0; i < lJornadas.length; i++){
            		if (lJornadas[i] == lTemporadas[0]){
            			datosJornada[cont]= Integer.toString(lJornadas[i+1]);
            			cont++;
            		}
            		
            		i++;
            	}
            }
            else
            {
            	datosJornada = new String[]{"1","2","3","4","5", "6", "7", "8", "9", "10", "11","12","13","14","15", "16", "17", "18", "19", "20", "21","22","23","24","25", "26", "27", "28", "29", "30", "31","32","33","34","35", "36", "37", "38", "39","40" };
            }
        	
        	datosTemporada = new String[lTemporadas.length];
			for (int i = 0; i < lTemporadas.length; i++)
			{
				datosTemporada[i] = Integer.toString(lTemporadas[i]);
			}
        }
        else
        {
        	datosTemporada = new String[]{"2012","2011","2010","2009","2008", "2007", "2006"};	
        }
        	
		
		//ArrayAdapter<String> adaptadorTemporada = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, datosTemporada);
        ArrayAdapter<String> adaptadorTemporada = new ArrayAdapter<String>(this, R.layout.textview_spinner, datosTemporada);
        
		cmbTemporada.setAdapter(adaptadorTemporada);    	   
        cmbTemporada.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    		public void onItemSelected(AdapterView<?> parent, android.view.View v, int position, long id) {
    			try{
    			appQG.setTemporadaSeleccionada(Integer.parseInt(datosTemporada[position]));
    			cargaSpinnerJornada(position);
    			}catch (Exception e)
    			{
    				e.printStackTrace();
    			}
    		}
    		public void onNothingSelected(AdapterView<?> parent) {
    			try{
    			appQG.setTemporadaSeleccionada(0);
    			}catch (Exception e)
    			{
    				e.printStackTrace();
    			}
    		}
    		});        
	   
        cargaSpinnerJornada(0);
        
        //Intentamos seleccionar la temporada y jornada actual
        //cmbTemporada.setSelection(adaptadorTemporada.getPosition(Integer.toString(datosGen.getTemporada())));
        //cmbJornada.setSelection(datosGen.getJornada());
        
		//Gestionamos el evento del volver
        final ImageView btnImagenVolver = (ImageView)findViewById(R.id.volverdeseleccionartemjor);
        btnImagenVolver.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
	            //Creamos el Intent
	        	Intent intent = null;
	        	switch (miOrigen){
	        	case 1: //appQG.ORIGEN_SELECCION_RANKING_EFECTIVIDAD
	        		intent = new Intent(QGSeleccionarTemporadaJornada.this, QGRankingEfectividad.class);
	        		break;
	        	case 2: //appQG.ORIGEN_SELECCION_RANKING_PUNTOS
	        		intent = new Intent(QGSeleccionarTemporadaJornada.this, QGRankingPuntos.class);
	        		break;
	        	case 3: //appQG.ORIGEN_SELECCION_QUINIELA_FINAL
	        		intent = new Intent(QGSeleccionarTemporadaJornada.this, QGQuinielaFinal.class);
	        		break;
	        	default:
	        		intent = new Intent(QGSeleccionarTemporadaJornada.this, QGPrincipal.class);
	        		break;
	        	}
	        	//Iniciamos la nueva actividad
	            startActivity(intent);
	            QGSeleccionarTemporadaJornada.this.finish();
			}
		});
        
        final Button btnSeleccionar = (Button)findViewById(R.id.buttonSeleccionarSeleccionarTemJor);
        btnSeleccionar.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				//Creamos el Intent
	        	Intent intent = null;
	        	
	        	switch (miOrigen){
	        	case 1: //appQG.ORIGEN_SELECCION_RANKING_EFECTIVIDAD
	        		intent = new Intent(QGSeleccionarTemporadaJornada.this, QGRankingEfectividad.class);
	        		break;
	        	case 2: //appQG.ORIGEN_SELECCION_RANKING_PUNTOS
	        		intent = new Intent(QGSeleccionarTemporadaJornada.this, QGRankingPuntos.class);
	        		break;
	        	case 3: //appQG.ORIGEN_SELECCION_RANKING_QUINIELA_FINAL
	        		intent = new Intent(QGSeleccionarTemporadaJornada.this, QGQuinielaFinal.class);
	        		break;
	        	default:
	        		intent = new Intent(QGSeleccionarTemporadaJornada.this, QGPrincipal.class);
	        		break;
	        	}
	        	//Iniciamos la nueva actividad
	        	appQG.bVenimosDeSeleccionarTemporadaJornada = true;
	        	appQG.bSeleccionadaTemporadaJornada = true;
	            startActivity(intent);
	            QGSeleccionarTemporadaJornada.this.finish();
			}
		});
		}catch (Exception e){
			mostrarMensaje("Hubo un error al Seleccionar Temporada");
            //Creamos el Intent
        	Intent intent = null;
        	switch (miOrigen){
        	case 1: //appQG.ORIGEN_SELECCION_RANKING_EFECTIVIDAD
        		intent = new Intent(QGSeleccionarTemporadaJornada.this, QGRankingEfectividad.class);
        		break;
        	case 2: //appQG.ORIGEN_SELECCION_RANKING_PUNTOS
        		intent = new Intent(QGSeleccionarTemporadaJornada.this, QGRankingPuntos.class);
        		break;
        	case 3: //appQG.ORIGEN_SELECCION_QUINIELA_FINAL
        		intent = new Intent(QGSeleccionarTemporadaJornada.this, QGQuinielaFinal.class);
        		break;
        	default:
        		intent = new Intent(QGSeleccionarTemporadaJornada.this, QGPrincipal.class);
        		break;
        	}
        	//Iniciamos la nueva actividad
            startActivity(intent);
            QGSeleccionarTemporadaJornada.this.finish();
		}
    }
    private void mostrarMensaje(String mensaje)
    {
    	Toast toast1 = Toast.makeText(getApplicationContext(),
                        mensaje, Toast.LENGTH_SHORT);
     		toast1.setGravity(Gravity.CENTER,0,0);
            toast1.show();
    }
}
